package cn.blunce.view_utils.ListViewHelp;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import cn.blunce.mylib.R;

public class ListViewTest extends Activity implements OnItemClickListener {

    private View layout;
    private ListView mListView;
    private RadioButton radioButton1;
    private RadioButton radioButton2;

    private List<String[]> datas = null;
    private FiveColmunAdapter mAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_test);

        initView();
        loadSenorData();
    }

    private void initView() {
        ((TextView) findViewById(R.id.label_1)).setText("执行器名称");

        mListView = (ListView) findViewById(R.id.content_show);
    }

    private void loadSenorData() {
        // TODO Auto-generated method stub
        datas = new ArrayList<String[]>();
        String[] item;
        for (int i = 0; i < 20; i++) {
            item = new String[]{"Excutor " + (i + 1), "20150304", "" + i % 2,
                    "" + i % 2, i % 2 == 0 ? "在线" : "离线"};
            datas.add(item);
        }
        mAdapter = new FiveColmunAdapter(this, datas);
        mListView.setAdapter(mAdapter);

        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                            final int position, long id) {
        // TODO Auto-generated method stub
        layout = LayoutInflater.from(this).inflate(R.layout.dialog,
                null);
        radioButton1 = (RadioButton) layout.findViewById(R.id.radio0);
        radioButton2 = (RadioButton) layout.findViewById(R.id.radio1);

        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle(
                        "设置" + ((String[]) mAdapter.getItem(position))[0]
                                + "状态").setView(layout)
                .setPositiveButton("确定", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        String[] item = (String[]) mAdapter.getItem(position);
                        if (radioButton1.isChecked()) {
                            item[2] = "0";
                            item[3] = "0";
                            item[4] = "离线";
                        }
                        if (radioButton2.isChecked()) {
                            item[2] = "1";
                            item[3] = "1";
                            item[4] = "在线";
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                }).setNegativeButton("取消", null).create();
        dialog.setCanceledOnTouchOutside(false);//设置dialog，使点击dialog外的区域时，dialog不消失
        dialog.show();
    }
}
